/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-08-10
 * Time: 21:50
 */
public class MyLinkedList {
    ListNode head;
    ListNode last;

    //打印双链表中的数据
    public void display() {
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //得到双链表的长度
    public int size() {
        ListNode cur = this.head;
        int size = 0;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        return size;
    }

    //判断双链表当中是否有包含key节点
    public boolean contains(int key){
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //头插法
    public void addFirst(int data){
        ListNode cur = new ListNode(data);
        if (this.head == null) {
            this.head = this.last = cur;
        }else {
            cur.next = this.head;
            this.head.prev = cur;
            this.head = cur;
        }
    }

    //尾插法
    public void addLast(int data){
        ListNode cur = new ListNode(data);
        if (this.head == null) {
            this.head = this.last = cur;
        }else {
            this.last.next = cur;
            cur.prev = this.last;
            this.last = cur;
        }
    }

    //判断插入位置是否合法
    private void isIllegal(int index) {
        if (index < 0 || index > size()) {
            throw new InsertIllegalException("插入位置不合法！");
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        try {
            isIllegal(index);
            int len = size();
            //头插
            if (index == 0) {
                addFirst(data);
                return;
            }
            //尾插
            if (index == len) {
                addLast(data);
                return;
            }
            //中间插入
            ListNode newN = new ListNode(data);
            ListNode cur = this.head;
            while (index != 0) {
                cur = cur.next;
                index--;
            }
            newN.next = cur;
            cur.prev.next = newN;
            newN.prev = cur.prev;
            cur.prev = newN;
        }catch (InsertIllegalException e) {
            e.printStackTrace();
        }
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if (this.head == null) {
            System.out.println("链表为空！");
            return;
        }
        ListNode cur = this.head;
        boolean mark = false;  //用于标记链表中是否有key，有的话置为true
        while (cur != null) {
            //先找key
            if (cur.val == key) {
                mark = true;
                //当cur位于头节点
                if (cur == this.head) {
                    this.head = this.head.next;
                    //当删除后链表不为空，即链表不仅仅有一个节点
                    if (this.head != null) {
                        this.head.prev = null;
                    }else {
                        //删除后链表为空，即链表仅有一个节点，这里更新尾节点last
                        this.last = null;
                    }
                }else {
                    //位于中间或者尾节点
                    cur.prev.next = cur.next;
                    //位于尾节点
                    if (cur.next == null) {
                        this.last = this.last.prev;
                    }else {
                        //位于中间
                        cur.next.prev = cur.prev;
                    }
                }
                //删除完成！
                return;
            }
            cur = cur.next;
        }
        if (mark == false) {
            System.out.println("链表中没有" + key);
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if (this.head == null) {
            System.out.println("链表为空！");
            return;
        }
        ListNode cur = this.head;
        boolean mark = false;  //用于标记链表中是否有key，有的话置为true
        while (cur != null) {
            ListNode curN = cur.next;
            //先找key
            if (cur.val == key) {
                mark = true;
                //当cur位于头节点
                if (cur == this.head) {
                    this.head = this.head.next;
                    //当删除后链表不为空，即链表不仅仅有一个节点
                    if (this.head != null) {
                        this.head.prev = null;
                    } else {
                        //删除后链表为空，即链表仅有一个节点，这里更新尾节点last
                        this.last = null;
                    }
                } else {
                    //位于中间或者尾节点
                    cur.prev.next = cur.next;
                    //位于尾节点
                    if (cur.next == null) {
                        this.last = this.last.prev;
                    } else {
                        //位于中间
                        cur.next.prev = cur.prev;
                    }
                }
            }
            cur = curN;
        }
        if (mark == false) {
            System.out.println("链表中没有" + key);
        }
    }

    //清空双链表
    public void clear() {
        ListNode cur = this.head;
        while (cur != null) {
            ListNode curN = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curN;
        }
        this.head = null;
        this.last = null;
    }
}
